//
//  proCateListController.swift
//  WxToutiao
//
//  Created by YQ05 on 2018/4/27.
//  Copyright © 2018年 yons. All rights reserved.
//

import UIKit

class proCateListController: UITableViewController {
    
    
    
//    @IBAction func tapStepperBuy(_ sender: UIStepper) {
//       textFieldBuy.text = Int(exactly: sender.value)?.description
//    }
    var proList:[Product] = []
    var parentNavi : UINavigationController?
    var id = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        
    getData()
        
        
        tableView.backgroundColor = UIColor(white: 0.98, alpha: 1) //表格背景颜色
        
        tableView.tableFooterView = UIView(frame: CGRect.zero)  //移除空行的分割线
        tableView.separatorColor = UIColor(white: 0.9, alpha: 1)  //分割线颜色
        
        tableView.estimatedRowHeight = 100  //预计行高
        tableView.rowHeight = UITableViewAutomaticDimension  //实际行高。启动自适应
        
     print("proCateListController  viewDidLoad 接收到的分类ID",self.self.id)
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
    }
    
    func getData()  {
        
       Product.request(id: id) { (data) in
            
            if let data = data {
                
                OperationQueue.main.addOperation {
                    self.proList = data
                    self.tableView.reloadData()
                    self.refreshControl?.endRefreshing()
                }
                
                print("proCateListController getData 接收到的分类ID",self.self.id)
                
            } else {
                print("proCateListController网络错误")
            }
        }
        tableView.estimatedRowHeight = 150
        tableView.rowHeight = UITableViewAutomaticDimension
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

//    override func numberOfSections(in tableView: UITableView) -> Int {
//        // #warning Incomplete implementation, return the number of sections
//        return 1
//    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return proList.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "proTableListCell", for: indexPath) as! proTableListCell

        let pros = proList[indexPath.row]

        cell.protitil.text = pros.name
          print("proname",pros.name)
        cell.Proprice.text = "$" + pros.regprice!

     
        
        
        //let originImg = "http://cc.cocimg.com/api/uploads/20141224/1419413840169392.png"
        let originImg = pros.image ?? "http://cc.cocimg.com/api/uploads/20141224/1419413840169392.png"
          //print("originImg:",originImg )
        let safeImg = originImg.addingPercentEncoding(withAllowedCharacters: NSCharacterSet(charactersIn:"`#%^{}\"[]|\\<> ").inverted)!

        /*从扩展函数拿图片*/
        if let url = URL(string: safeImg) {
            cell.proimg.downloadedFrom(url: url)
        }
        /*从扩展函数拿图片*/

        // Configure the cell...

        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let pros = proList[tableView.indexPathForSelectedRow!.row]


        let detailVC = storyboard?.instantiateViewController(withIdentifier: "SBidhNewsDetail") as! hNewsDetailController

        //detailVC.title = title! + "新闻详情"

        detailVC.title = pros.name
        //detailVC.post = pros


        parentNavi?.pushViewController(detailVC, animated: true)
    }

    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
